package mest.controller.hws;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.alibaba.fastjson.JSON;
import mest.entity.device_file;
import mest.service.hws.DeviceFilehwsService;
import mest.service.system.DBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/*
* 设备档案
* */
@RestController
@RequestMapping("/deviceFile")
@CrossOrigin
public class DeviceFileController {

    @Resource
    private DeviceFilehwsService deviceFileService;
    @Autowired
    private DBService dbService;

/*    @Resource
    private ModelingDictionariesService modelingDictionariesService;*/

    //查询全部设备档案
    @RequestMapping("/selectAll")
    public String selectAllh (Integer limit,Integer page,String devicename){
        System.out.println("查询全部设备档案");
        String theirDb = dbService.getCompanyDB();
        Map<Object,Object> map = new HashMap<Object,Object>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",deviceFileService.selectPrimarycount(theirDb,devicename));
        map.put("data",deviceFileService.selectPrimary(theirDb,limit,(page-1)*limit,devicename));
        System.out.println((page - 1) * limit);
        return JSON.toJSONString(map);
    }

    //根据id查询设备档案
    @RequestMapping("/selectById")
    public String selectByIdh(Integer id){
        System.out.println("根据id查询设备档案");
        String theirDb = dbService.getCompanyDB();
        Map<Object,Object> map = new HashMap<Object,Object>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",1);
        map.put("data",deviceFileService.selectByPrimaryKey(theirDb,id));
        return JSON.toJSONString(map);
    }



    //增加设备档案
    @RequestMapping("/insertModel")
    public String insertModelh(device_file deviceFile ,String BuyingTime1){
       /* deviceFile.setBuyingTime(new Date(System.currentTimeMillis()));
        System.out.println(new Date(System.currentTimeMillis()));*/
        System.out.println("增加设备档案");
        String theirDb = dbService.getCompanyDB();
        System.out.println(BuyingTime1);
        System.out.println(deviceFile);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date dateTime = null;
        try {
            dateTime = simpleDateFormat.parse(BuyingTime1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        deviceFile.setBuyingTime(dateTime);

        Map<String, Object> map = new HashMap<String, Object>();
        return deviceFileService.insert(theirDb,deviceFile);
    }

  /*  //设备档案编码生成
    @RequestMapping("/Number")
    public String Numberh(){
        Map<Object,Object> map = new HashMap<Object,Object>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count","count");
        map.put("data","SB-"+modelingDictionariesService.selectModelingDictionaries().getDevicefileNo());
        return JSON.toJSONString(map);
    }*/


    //更新设备档案
    @RequestMapping("/updateByone")
    public String updateByoneh(@RequestBody device_file deviceFile){
        System.out.println("更新设备档案");
        String theirDb = dbService.getCompanyDB();
        System.out.println(deviceFile.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        return deviceFileService.updateByPrimaryKey(theirDb,deviceFile);
    }

    //删除设备档案
    @RequestMapping("/delModel")
    public String delModelh(Integer id){
        System.out.println("删除设备档案");
        String theirDb = dbService.getCompanyDB();
        Map<String, Object> map = new HashMap<String, Object>();
        int num = deviceFileService.deleteByPrimaryKey(theirDb,id);
        if (num > 0) {
            map.put("code", 0);
            map.put("msg", "操作成功");
        } else {
            map.put("code", 1);
            map.put("msg", "操作失败");
        }
        return JSON.toJSONString(map);
    }


}
